// 洗牌函数
const shuffle = (r) =>
	r
		.map((a) => ({ rand: Math.random(), value: a }))
		.sort((a, n) => a.rand - n.rand)
		.map((a) => a.value);

const debounce = ({ delay: l }, o) => {
	let e,
		d = !0;
	const t = (...i) => {
		d ?
			(clearTimeout(e),
			(e = setTimeout(() => {
				d && o(...i), (e = void 0);
			}, l)))
		:	o(...i);
	};
	return (
		(t.isPending = () => e !== void 0),
		(t.cancel = () => {
			d = !1;
		}),
		(t.flush = (...i) => o(...i)),
		t
	);
};

const throttle = ({ interval: o }, i) => {
	let t = !0,
		e;
	const r = (...d) => {
		t &&
			(i(...d),
			(t = !1),
			(e = setTimeout(() => {
				(t = !0), (e = void 0);
			}, o)));
	};
	return (r.isThrottled = () => e !== void 0), r;
};

export { shuffle, throttle, debounce };
